package com.thinktank.controller;

import static com.thinktank.util.DbUtil.*;

import java.io.*;
import java.sql.*;

import javax.servlet.http.*;

import com.thinktank.dao.*;
import com.thinktank.vo.*;

public class MemberJoinController implements Controller {
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		// 인코딩 타입 세팅 & 정보 받기
		request.setCharacterEncoding("utf-8");
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		String day = request.getParameter("day");
		String gender = request.getParameter("gender");	// 남 : male    여 : female
		String page = request.getParameter("page");
		
		// 멤버 생성후 정보 입력
		Member member = new Member();
		member.setmMail(email);
		member.setmName(name);
		member.setmPass(password);
		String mDate = year + "/" + month + "/" + day;
		member.setmDate(mDate);
		String mGender="n";
		if(gender.equals("male")) mGender="남";
		if(gender.equals("female")) mGender="여";
		member.setmGender(mGender);
		member.setmPage(page);
		
		// DB 작업
		Connection con = getConnection();
		MemberDao dao = new MemberDao(con);
		int result=0;
		try {
			result = dao.InsertMember(member);
			member = dao.login(member.getmMail(), member.getmPass());
			close(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(result == 0){
			return "error.jsp";
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("member", member);
		session.setAttribute("url", member.getmPage());
		request.setAttribute("p", 1);
		
		
		return "redirect:home/home.do?p=1";
	}

}
